<!doctype html>
<html lang="zh-Hans">
	<head>
		<title>电阻串并联计算器 - 嵌入式工具集</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<!-- 以下为浏览器中的配置，正式 编译之后请注释掉这段-->
		<link rel="stylesheet" href="../styles/styles.css" />
		<!-- 以下为LCEDA中的配置，正式 编译之后请勿注释这段-->
		<link rel="stylesheet" href="/iframe/styles/styles.css" />

		<style>
			/* 电阻串并联计算器专用样式 */
			.calculator-container {
				background: white;
				border-radius: 12px;
				box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
			}

			.calculation-type {
				background-color: #f8fafc;
				border-radius: 8px;
				padding: 1rem;
				margin-bottom: 1.5rem;
			}

			.radio-group {
				display: flex;
				justify-content: center;
				gap: 2rem;
				margin: 1rem 0;
			}

			.radio-label {
				display: flex;
				align-items: center;
				justify-content: center;
				cursor: pointer;
				transition: all 0.2s ease;
			}

			/* 选中状态样式 */
			input[type='radio']:checked + span {
				font-weight: 600;
			}

			input[type='radio']:checked + span {
				color: #4a90e2;
			}

			.radio-label:hover {
				color: #4a90e2;
			}

			.resistor-inputs {
				margin-bottom: 1.5rem;
			}

			.resistor-input-group {
				display: flex;
				align-items: stretch;
				gap: 0;
				margin-bottom: 0.75rem;
				background-color: #f8fafc;
				border-radius: 8px;
				overflow: hidden;
				transition: all 0.2s ease;
			}

			.resistor-input-group:hover {
				background-color: #f1f5f9;
				box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
			}

			.resistor-label {
				background-color: #4a90e2;
				color: white;
				padding: 0.75rem 1rem;
				font-weight: 600;
				min-width: 50px;
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
			}

			.resistor-input {
				flex: 1;
				position: relative;
				display: flex;
				align-items: center;
			}

			.resistor-value {
				width: 100%;
				padding: 0.75rem 3rem 0.75rem 1rem;
				border: none;
				background: transparent;
				font-size: 1rem;
				outline: none;
				color: #374151;
			}

			.resistor-value:focus {
				background-color: white;
			}

			.unit-selector {
				position: absolute;
				right: 0.75rem;
				top: 50%;
				transform: translateY(-50%);
				background: transparent;
				border: 1px solid #e2e8f0;
				border-radius: 4px;
				font-size: 0.875rem;
				color: #6b7280;
				cursor: pointer;
				padding: 0.25rem 0.5rem;
				transition: all 0.2s ease;
			}

			.unit-selector:hover {
				background-color: white;
				border-color: #cbd5e1;
				color: #475569;
			}

			.btn-add {
				background-color: #10b981;
				color: white;
				border: none;
				border-radius: 6px;
				padding: 0.5rem 1rem;
				cursor: pointer;
				transition: all 0.2s ease;
				font-weight: 500;
				display: block;
				width: 100%;
				margin-bottom: 0.5rem;
			}

			.btn-add:hover {
				background-color: #059669;
				transform: translateY(-1px);
			}

			.btn-remove {
				background-color: #ef4444;
				color: white;
				border: none;
				cursor: pointer;
				transition: all 0.2s ease;
				min-width: 50px;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
			}

			.btn-remove:hover {
				background-color: #dc2626;
			}

			.btn-remove:disabled {
				background-color: #f3f4f6;
				color: #9ca3af;
				cursor: not-allowed;
			}

			.result-card {
				background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
				color: white;
				border-radius: 12px;
				padding: 1.5rem;
				text-align: center;
				margin-bottom: 1.5rem;
				transition: transform 0.2s ease;
			}

			.result-card:hover {
				transform: translateY(-2px);
				box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
			}

			.result-value {
				font-size: 2rem;
				font-weight: 700;
				margin: 0.5rem 0;
			}

			.btn-reset {
				background-color: #f5f5f5;
				color: #555;
				border: none;
				border-radius: 8px;
				padding: 0.75rem;
				font-size: 1rem;
				font-weight: 500;
				cursor: pointer;
				transition: all 0.2s ease;
				display: block;
				width: 100%;
			}

			.btn-reset:hover {
				background-color: #e0e0e0;
				transform: translateY(-1px);
			}

			.formula-display {
				background-color: #f8fafc;
				border-radius: 8px;
				padding: 1rem;
				margin-bottom: 1.5rem;
				font-family: monospace;
				text-align: center;
				border: 1px solid #e2e8f0;
			}

			.formula-title {
				font-weight: bold;
				margin-bottom: 0.5rem;
				color: #475569;
			}

			.formula-text {
				color: #334155;
				font-size: 0.875rem;
			}

			/* 响应式设计 */
			@media (max-width: 640px) {
				/* 确保在嘉立创EDA插件环境中保持左右布局 */
				.calculation-type .flex.justify-between {
					flex-direction: row !important;
					gap: 0 !important;
				}

				.calculation-type .radio-label {
					border-radius: 0 !important;
					margin-bottom: 0 !important;
				}

				.result-value {
					font-size: 1.5rem;
				}
			}
		</style>
	</head>
	<body class="bg-gradient-to-br from-light to-blue-50 min-h-screen p-4 md:p-6 font-sans">
		<div class="max-w-md mx-auto bg-white rounded-2xl shadow-lg p-6 md:p-8">
			<h2 class="text-2xl md:text-3xl font-bold text-primary mb-6 text-center">电阻串并联计算器</h2>

			<div class="calculation-type">
				<label class="block text-sm font-medium text-gray-700 mb-3 text-center">选择计算类型</label>
				<div class="flex justify-between">
					<label
						class="radio-label w-full text-center py-2 bg-gray-100 rounded-l-lg border-2 border-gray-200 hover:bg-gray-200 transition-colors"
					>
						<input type="radio" name="calc-type" value="series" class="form-radio h-5 w-5 text-accent" checked />
						<span class="ml-2">串联</span>
					</label>
					<label
						class="radio-label w-full text-center py-2 bg-gray-100 rounded-r-lg border-2 border-gray-200 hover:bg-gray-200 transition-colors"
					>
						<input type="radio" name="calc-type" value="parallel" class="form-radio h-5 w-5 text-accent" />
						<span class="ml-2">并联</span>
					</label>
				</div>
			</div>

			<div class="formula-display">
				<div class="formula-title">计算公式</div>
				<div class="formula-text" id="formula-text">串联: R<sub>总</sub> = R<sub>1</sub> + R<sub>2</sub> + ... + R<sub>n</sub></div>
			</div>

			<div class="resistor-inputs" id="resistor-inputs">
				<div class="resistor-input-group">
					<div class="resistor-label">R1</div>
					<div class="resistor-input">
						<input type="number" class="resistor-value" placeholder="0" min="0" step="0.01" value="1000" />
						<select class="unit-selector">
							<option value="ohm">Ω</option>
							<option value="kilo-ohm">kΩ</option>
							<option value="mega-ohm">MΩ</option>
						</select>
					</div>
					<button class="btn-remove" onclick="removeResistor(this)" disabled>✕</button>
				</div>

				<div class="resistor-input-group">
					<div class="resistor-label">R2</div>
					<div class="resistor-input">
						<input type="number" class="resistor-value" placeholder="0" min="0" step="0.01" value="2000" />
						<select class="unit-selector">
							<option value="ohm">Ω</option>
							<option value="kilo-ohm">kΩ</option>
							<option value="mega-ohm">MΩ</option>
						</select>
					</div>
					<button class="btn-remove" onclick="removeResistor(this)">✕</button>
				</div>
			</div>

			<button class="btn-add" onclick="addResistor()">➕ 添加电阻</button>

			<div class="result-card">
				<h3 class="font-bold mb-2">等效电阻</h3>
				<div class="result-value" id="result-value">3000 Ω</div>
			</div>

			<button class="btn-reset" onclick="resetCalculator()">🔄 重置</button>
		</div>

		<script>
			// 初始化计算器
			function initResistorsCalculator() {
				// 绑定计算类型切换事件
				const calcTypeRadios = document.querySelectorAll('input[name="calc-type"]');
				calcTypeRadios.forEach((radio) => {
					radio.addEventListener('change', updateFormula);
				});

				// 绑定所有输入事件
				const inputs = document.querySelectorAll('.resistor-value, .unit-selector');
				inputs.forEach((input) => {
					input.addEventListener('input', calculate);
				});

				// 更新公式显示
				function updateFormula() {
					const formulaText = document.getElementById('formula-text');
					const isSeries = document.querySelector('input[name="calc-type"][value="series"]').checked;

					if (isSeries) {
						formulaText.innerHTML = '串联: R<sub>总</sub> = R<sub>1</sub> + R<sub>2</sub> + ... + R<sub>n</sub>';
					} else {
						formulaText.innerHTML = '并联: 1/R<sub>总</sub> = 1/R<sub>1</sub> + 1/R<sub>2</sub> + ... + 1/R<sub>n</sub>';
					}

					calculate();
				}

				// 计算等效电阻
				function calculate() {
					const isSeries = document.querySelector('input[name="calc-type"][value="series"]').checked;
					const inputGroups = document.querySelectorAll('.resistor-input-group');
					let totalResistance = 0;

					if (isSeries) {
						// 串联计算
						inputGroups.forEach((group) => {
							const value = parseFloat(group.querySelector('.resistor-value').value) || 0;
							const unit = group.querySelector('.unit-selector').value;
							const resistance = convertToOhms(value, unit);
							totalResistance += resistance;
						});
					} else {
						// 并联计算
						let reciprocalSum = 0;
						inputGroups.forEach((group) => {
							const value = parseFloat(group.querySelector('.resistor-value').value) || 0;
							const unit = group.querySelector('.unit-selector').value;
							const resistance = convertToOhms(value, unit);
							if (resistance > 0) {
								reciprocalSum += 1 / resistance;
							}
						});
						totalResistance = reciprocalSum > 0 ? 1 / reciprocalSum : 0;
					}

					// 格式化结果显示
					displayResult(totalResistance);
				}

				// 转换为欧姆单位
				function convertToOhms(value, unit) {
					switch (unit) {
						case 'kilo-ohm':
							return value * 1000;
						case 'mega-ohm':
							return value * 1000000;
						case 'ohm':
						default:
							return value;
					}
				}

				// 格式化并显示结果
				function displayResult(resistance) {
					const resultElement = document.getElementById('result-value');
					let displayValue, unit;

					if (resistance >= 1000000) {
						displayValue = (resistance / 1000000).toFixed(3);
						unit = 'MΩ';
					} else if (resistance >= 1000) {
						displayValue = (resistance / 1000).toFixed(3);
						unit = 'kΩ';
					} else {
						displayValue = resistance.toFixed(2);
						unit = 'Ω';
					}

					// 移除尾随零和小数点
					displayValue = parseFloat(displayValue).toString();

					// 添加结果更新的动画效果
					resultElement.style.opacity = '0.5';
					setTimeout(() => {
						resultElement.textContent = displayValue + ' ' + unit;
						resultElement.style.opacity = '1';
					}, 150);
				}

				// 返回公开方法
				return {
					calculate: calculate,
					updateFormula: updateFormula,
				};
			}

			// 添加电阻输入
			function addResistor() {
				const container = document.getElementById('resistor-inputs');
				const inputGroups = container.querySelectorAll('.resistor-input-group');
				const resistorNumber = inputGroups.length + 1;

				// 创建新的输入组
				const newGroup = document.createElement('div');
				newGroup.className = 'resistor-input-group';
				newGroup.innerHTML = `
					<div class="resistor-label">R${resistorNumber}</div>
					<div class="resistor-input">
						<input type="number" class="resistor-value" placeholder="0" min="0" step="0.01" value="1000">
						<select class="unit-selector">
							<option value="ohm">Ω</option>
							<option value="kilo-ohm">kΩ</option>
							<option value="mega-ohm">MΩ</option>
						</select>
					</div>
					<button class="btn-remove" onclick="removeResistor(this)">
						✕
					</button>
				`;

				// 添加到容器
				container.appendChild(newGroup);

				// 启用所有删除按钮
				const removeButtons = document.querySelectorAll('.btn-remove');
				removeButtons.forEach((button) => {
					button.disabled = false;
				});

				// 绑定事件
				const newValueInput = newGroup.querySelector('.resistor-value');
				const newUnitSelect = newGroup.querySelector('.unit-selector');
				newValueInput.addEventListener('input', () => {
					const calculator = initResistorsCalculator();
					calculator.calculate();
				});
				newUnitSelect.addEventListener('change', () => {
					const calculator = initResistorsCalculator();
					calculator.calculate();
				});

				// 重新计算
				const calculator = initResistorsCalculator();
				calculator.calculate();
			}

			// 移除电阻输入
			function removeResistor(button) {
				const container = document.getElementById('resistor-inputs');
				const inputGroups = container.querySelectorAll('.resistor-input-group');

				// 确保至少保留一个输入
				if (inputGroups.length > 1) {
					const groupToRemove = button.closest('.resistor-input-group');
					container.removeChild(groupToRemove);

					// 如果只剩一个输入，禁用删除按钮
					const remainingGroups = container.querySelectorAll('.resistor-input-group');
					if (remainingGroups.length === 1) {
						remainingGroups[0].querySelector('.btn-remove').disabled = true;
					}

					// 更新剩余电阻的代号
					remainingGroups.forEach((group, index) => {
						const label = group.querySelector('.resistor-label');
						label.textContent = `R${index + 1}`;
					});

					// 重新计算
					const calculator = initResistorsCalculator();
					calculator.calculate();
				}
			}

			// 重置计算器
			function resetCalculator() {
				const container = document.getElementById('resistor-inputs');

				// 清空所有输入组
				container.innerHTML = '';

				// 添加默认的两个输入组
				const defaultGroups = [
					{ value: 1000, unit: 'ohm' },
					{ value: 2000, unit: 'ohm' },
				];

				defaultGroups.forEach((group, index) => {
					const newGroup = document.createElement('div');
					newGroup.className = 'resistor-input-group';
					newGroup.innerHTML = `
						<div class="resistor-label">R${index + 1}</div>
						<div class="resistor-input">
							<input type="number" class="resistor-value" placeholder="0" min="0" step="0.01" value="${group.value}">
							<select class="unit-selector">
								<option value="ohm" ${group.unit === 'ohm' ? 'selected' : ''}>Ω</option>
								<option value="kilo-ohm" ${group.unit === 'kilo-ohm' ? 'selected' : ''}>kΩ</option>
								<option value="mega-ohm" ${group.unit === 'mega-ohm' ? 'selected' : ''}>MΩ</option>
							</select>
						</div>
						<button class="btn-remove" onclick="removeResistor(this)" ${index === 0 ? 'disabled' : ''}>
							✕
						</button>
					`;
					container.appendChild(newGroup);

					// 绑定事件
					const newValueInput = newGroup.querySelector('.resistor-value');
					const newUnitSelect = newGroup.querySelector('.unit-selector');
					newValueInput.addEventListener('input', () => {
						const calculator = initResistorsCalculator();
						calculator.calculate();
					});
					newUnitSelect.addEventListener('change', () => {
						const calculator = initResistorsCalculator();
						calculator.calculate();
					});
				});

				// 重置计算类型为串联
				document.querySelector('input[name="calc-type"][value="series"]').checked = true;

				// 重新初始化
				const calculator = initResistorsCalculator();
				calculator.updateFormula();
				calculator.calculate();
			}

			// 页面加载时初始化
			window.onload = function () {
				const calculator = initResistorsCalculator();
				calculator.calculate();
			};

			// 暴露给父页面的接口
			window.addResistor = addResistor;
			window.removeResistor = removeResistor;
			window.resetCalculator = resetCalculator;
		</script>

		<!-- 页脚 -->
		<footer class="max-w-md mx-auto mt-8 text-center text-sm text-gray-500">
			<p>版本号: V0.0.1-20251004</p>
		</footer>
	</body>
</html>
